//
//  GameScene.m
//  Bridge
//
//  Created by vinh pham on 5/11/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "GameScene.h"
#import "MainScene.h"
#import "CCFileUtils.h"
#import "SHK.h"
#import "SHKTwitter.h"
#import "SHKFacebook.h"

@implementation HighscoreLayer

- (id) initWithScore:(int)score_ {
    self = [super init];
    if (self != nil) {
        CGSize winSize = [CCDirector sharedDirector].winSize;
        
        //isTouchEnabled_ = TRUE;
        
        CCSprite *bg = [CCSprite spriteWithFile:[CCFileUtils fullPathFromRelativePath:@"optionbg.png"]];
        [bg setPosition:ccp(winSize.width/2,winSize.height/2)];
        [self addChild:bg];
        
        CCLabelBMFont *lbScoreText = [CCLabelBMFont labelWithString:@"Score:" fntFile:FONT_FILE];
        [lbScoreText setPosition:ccp(winSize.width/2 - 70,winSize.height/2 + 50)];
        [self addChild:lbScoreText];
        
        CCLabelBMFont *lbScore = [CCLabelBMFont labelWithString:@"2402" fntFile:FONT_FILE];
        [lbScore setPosition:ccp(lbScoreText.position.x+150,lbScoreText.position.y)];
        [self addChild:lbScore];
        
        CCLabelBMFont *replayLabel = [CCLabelBMFont labelWithString:@"Replay" fntFile:FONT_FILE];    
        CCLabelBMFont *quitLabel = [CCLabelBMFont labelWithString:@"Back to Main Screen" fntFile:FONT_FILE];
                                    
        CCMenuItemImage *btnFb = [CCMenuItemImage itemFromNormalImage:@"fb_icon.png" selectedImage:@"fb_icon_highlight.png" target:self selector:@selector(shareFacebook:)];
        [btnFb setPosition:ccp(winSize.width/2-50,150)];
        
        CCMenuItemImage *btnTwitter = [CCMenuItemImage itemFromNormalImage:@"tweet_icon.png" selectedImage:@"tweet_icon_highlight.png" target:self selector:@selector(shareTwitter:)];
        [btnTwitter setPosition:ccp(winSize.width/2+50,150)];

        
        CCMenuItemLabel *btnReplay = [CCMenuItemLabel itemWithLabel:replayLabel target:self selector:@selector(replay:)];
        btnReplay.position = ccp(winSize.width/2, 110);
        
        CCMenuItemLabel *btnQuit = [CCMenuItemLabel itemWithLabel:quitLabel target:self selector:@selector(quit:)];
        btnQuit.position = ccp(winSize.width/2, 60);
        
        CCMenu *menu = [CCMenu menuWithItems:btnFb,btnTwitter,btnReplay,btnQuit,nil];
        menu.position = CGPointZero;
        [self addChild:menu];
        
        [btnReplay runAction:[CCScaleTo actionWithDuration:0.5 scale:1.0]];
        [btnQuit runAction:[CCScaleTo actionWithDuration:0.5 scale:1.0]];
        
        UIViewController *tempVC = [[UIViewController alloc] init];
        
        [[[CCDirector sharedDirector] openGLView] addSubview:tempVC.view];
        
        [[SHK currentHelper] setRootViewController:tempVC];

    }
    return self;
}

- (void) replay:(id)sender {
    [[CCDirector sharedDirector] replaceScene:[CCTransitionFadeTR transitionWithDuration:1.0 scene:[GameScene node]]];
}

- (void) quit:(id)sender {
    [[CCDirector sharedDirector] replaceScene:[CCTransitionFadeTR transitionWithDuration:1.0 scene:[MainScene node]]];
}
/*
- (void) postScore:(id)sender {
    SHKItem *item = [SHKItem text:@"OK"];
    
    //Create the sharekit action sheet
    SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];
    
    UIViewController *tempVC = [[UIViewController alloc] init];
    
    [[[CCDirector sharedDirector] openGLView] addSubview:tempVC.view];
    
    [[SHK currentHelper] setRootViewController:tempVC];
    
    [actionSheet showInView:[CCDirector sharedDirector].openGLView ];
}
*/
- (void) shareFacebook:(id)sender {
    SHKItem *fbItem = [SHKItem text:@"Facebook"];
    [SHKFacebook shareItem:fbItem];
}

- (void) shareTwitter:(id)sender {
    SHKItem *fbItem = [SHKItem text:@"Tweet"];
    [SHKTwitter shareItem:fbItem];
}

@end

@implementation MenuOption

- (id) init {
    self = [super init];
    if (self != nil) {
        CGSize winSize = [CCDirector sharedDirector].winSize;
        
        //isTouchEnabled_ = TRUE;
        
        CCSprite *bg = [CCSprite spriteWithFile:[CCFileUtils fullPathFromRelativePath:@"optionbg.png"]];
        [bg setPosition:ccp(winSize.width/2,winSize.height/2)];
        [self addChild:bg];
        
        //CCLabelBMFont *bgmLabel = [CCLabelBMFont labelWithString:@"BGM" fntFile:FONT_FILE];
        //[bgmLabel setPosition:ccp(winSize.width/2,240)];
        //[self addChild:bgmLabel];
        
        /* 
		 -- create BGM toogle menu --
		 */
        /*
		CCMenuItemImage *btnBGOn  = [CCMenuItemImage itemFromNormalImage:[CCFileUtils fullPathFromRelativePath:@"btn_on.png"] selectedImage:[CCFileUtils fullPathFromRelativePath:@"btn_on.png"]];
		CCMenuItemImage *btnBGOff = [CCMenuItemImage itemFromNormalImage:[CCFileUtils fullPathFromRelativePath:@"btn_off.png"] selectedImage:[CCFileUtils fullPathFromRelativePath:@"btn_off.png"]];
		CCMenuItemToggle *toogle1  = [CCMenuItemToggle itemWithTarget:self selector:@selector(switchBGM:) items:btnBGOff,btnBGOn,nil];
		
		CCMenu *menuBGM = [CCMenu menuWithItems:toogle1,nil];
		[menuBGM setPosition:ccp(bgmLabel.position.x+100,bgmLabel.position.y)];
		[self addChild:menuBGM];
		
        CCLabelBMFont *seLabel = [CCLabelBMFont labelWithString:@"SE" fntFile:FONT_FILE];
        [seLabel setPosition:ccp(winSize.width/2,160)];
        [self addChild:seLabel];
        */
        /*
		 -- create SE toogle menu --
		 */
		/*
		CCMenuItemImage *btnSEOn  = [CCMenuItemImage itemFromNormalImage:[CCFileUtils fullPathFromRelativePath:@"btn_on.png"] selectedImage:[CCFileUtils fullPathFromRelativePath:@"btn_on.png"]];
		CCMenuItemImage *btnSEOff = [CCMenuItemImage itemFromNormalImage:[CCFileUtils fullPathFromRelativePath:@"btn_off.png"] selectedImage:[CCFileUtils fullPathFromRelativePath:@"btn_off.png"]];
		CCMenuItemToggle *toogle2  = [CCMenuItemToggle itemWithTarget:self selector:@selector(switchSE:) items:btnSEOff,btnSEOn,nil];
		
		CCMenu *menuSE = [CCMenu menuWithItems:toogle2,nil];
		[menuSE setPosition:ccp(seLabel.position.x+100,seLabel.position.y)];
		[self addChild:menuSE];
        */
        //cancel
        CCLabelBMFont *replayLabel = [CCLabelBMFont labelWithString:@"Replay" fntFile:FONT_FILE];    
        CCLabelBMFont *cancelLabel = [CCLabelBMFont labelWithString:@"Cancel" fntFile:FONT_FILE];    
        CCLabelBMFont *quitLabel = [CCLabelBMFont labelWithString:@"Back to Main Screen" fntFile:FONT_FILE];    
        CCMenuItemLabel *replayItem = [CCMenuItemLabel itemWithLabel:replayLabel target:self selector:@selector(replay:)];
        replayItem.position = ccp(winSize.width/2, 200);
        
        CCMenuItemLabel *cancelItem = [CCMenuItemLabel itemWithLabel:cancelLabel target:self selector:@selector(cancel:)];
        cancelItem.position = ccp(winSize.width/2, 160);
        
        CCMenuItemLabel *quitItem = [CCMenuItemLabel itemWithLabel:quitLabel target:self selector:@selector(quit:)];
        quitItem.position = ccp(winSize.width/2, 120);
        
        CCMenu *menu = [CCMenu menuWithItems:replayItem,cancelItem,quitItem,nil];
        menu.position = CGPointZero;
        [self addChild:menu];
        
        [replayItem runAction:[CCScaleTo actionWithDuration:0.5 scale:1.0]];
        [cancelItem runAction:[CCScaleTo actionWithDuration:0.5 scale:1.0]];
        [quitItem runAction:[CCScaleTo actionWithDuration:0.5 scale:1.0]];
    }
    return self;
}

- (void) switchBGM:(id)sender {
    
}

- (void) switchSE:(id)sender {
    
}

- (void) cancel:(id)sender {
    [self removeFromParentAndCleanup:YES];
}

- (void) quit:(id)sender {
    [[CCDirector sharedDirector] replaceScene:[CCTransitionFadeTR transitionWithDuration:1.0f scene:[MainScene node]]];
}

- (void) replay:(id)sender {
    [[CCDirector sharedDirector] replaceScene:[CCTransitionFadeTR transitionWithDuration:1.0f scene:[GameScene node]]];
}

- (void) dealloc {
    [super dealloc];
}

@end

@implementation GameScene

- (id) init {
    self = [super init];
    if (self != nil) {
        CGSize winSize = [CCDirector sharedDirector].winSize;
        CCSprite *bg = [CCSprite spriteWithFile:[CCFileUtils fullPathFromRelativePath:@"bg_game.png"]];
        [bg setPosition:ccp(winSize.width/2,winSize.height/2)];
        [self addChild:bg];
        
        CCLabelBMFont *winLabel  = [CCLabelBMFont labelWithString:@"Win" fntFile:FONT_FILE];    
        CCLabelBMFont *loseLabel = [CCLabelBMFont labelWithString:@"Lose" fntFile:FONT_FILE];    
        CCLabelBMFont *menuLabel = [CCLabelBMFont labelWithString:@"Menu" fntFile:FONT_FILE];    
        
        CCMenuItemLabel *winItem = [CCMenuItemLabel itemWithLabel:winLabel target:self selector:@selector(win:)];
        winItem.position = ccp(winSize.width/2, winSize.height/2);
        
        CCMenuItemLabel *loseItem = [CCMenuItemLabel itemWithLabel:loseLabel target:self selector:@selector(lose:)];
        loseItem.position = ccp(winSize.width/2, winSize.height/2-50);

        CCMenuItemLabel *menuOption = [CCMenuItemLabel itemWithLabel:menuLabel target:self selector:@selector(menuOption:)];
        menuOption.position = ccp(winSize.width/2, winSize.height/2-100);
        
        CCMenu *menu = [CCMenu menuWithItems:winItem,loseItem,menuOption,nil];
        menu.position = CGPointZero;
        [self addChild:menu];
        
        [winItem runAction:[CCScaleTo actionWithDuration:0.5 scale:1.0]];
        [loseItem runAction:[CCScaleTo actionWithDuration:0.5 scale:1.0]];
        [menuOption runAction:[CCScaleTo actionWithDuration:0.5 scale:1.0]];
    }
    return self;
}

- (void) win:(id)sender {
    HighscoreLayer *layer = [[[HighscoreLayer alloc] initWithScore:0] autorelease];
    [self addChild:layer];
}

- (void) lose:(id)sender {
    
}

- (void) menuOption:(id)sender {
    [self addChild:[MenuOption node]];
}

- (void) dealloc {
    [super dealloc];
}

@end
